package com.yfbao.web.gateway.utils;

import java.net.URI;
import java.net.URISyntaxException;

public class CommonUtil {
    /**
     * 使用URI构建器（推荐）
     */
    public static String buildUrl(String baseUrl, String path) {
        try {
            URI baseUri = new URI(baseUrl);
            // 确保路径正确处理，避免丢失端口信息
            String basePath = baseUri.getPath();
            if (basePath == null) basePath = "";
            String finalPath = basePath + (path.startsWith("/") ? path : "/" + path);

            return new URI(
                    baseUri.getScheme(),
                    baseUri.getUserInfo(),
                    baseUri.getHost(),
                    baseUri.getPort(),
                    finalPath,
                    baseUri.getQuery(),
                    baseUri.getFragment()
            ).toString();
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("无效的URL: " + baseUrl, e);
        }
    }
}
